import {deepClone} from "@/libs/utils/ObjectClone";

const defaultConfigs={
    dic:{
        dicKey:''
    },
    requestParam:{
        serviceName:'',
        uri:'',
        params:'',
        method:''
    }
}


const validateRules={
    dic:{
        dicKey: [
            { required: true, message: '请输入字典编号', trigger: 'blur' },
        ]
    },
    requestParam:{
        serviceName: [
            { required: true, message: '请求服务名不能为空', trigger: 'blur' },
        ],
        uri: [
            { required: true, message: '请求路径不能为空', trigger: 'blur' },
        ],
        params: [
            {
                validator: (rule, value, callback)=>{
                    try{
                        if(value!=='') {
                            if (typeof value == 'string') {
                                let obj= JSON.parse(value);
                                if(typeof obj == 'object' && obj ){
                                    callback();
                                }else{
                                    callback(new Error('附加参数格式不正确(JSON格式数据)'));
                                }
                            }else{
                                callback(new Error('附加参数格式不正确(JSON格式数据)'));
                            }
                        }else{
                            callback(new Error('请求参数格式不正确(JSON格式数据)'));
                        }
                        callback();
                    }catch (e) {
                        callback(new Error('请求参数格式不正确(JSON格式数据)'));
                    }
                },
                trigger: 'blur'
            },
        ],
        method: [
            { required: true, message: '请求类型不能为空', trigger: 'change' },
        ]
    }
}

function genComponentConfig(type,config) {
    const copy = deepClone(defaultConfigs[type]);
    Object.keys(copy).forEach(key=>{
        if(!config[key]){
            config[key] = copy[key];
        }
    })
    return config;
}


function getValidateRules(type) {
    return validateRules[type];
}
const allParamTypes =[{title:'数据字典',value:'dic'},{title:'自定义字典',value:'custDic'},{title:'地址数据',value:'area'},{title:'输入值',value: 'input'},{title:'级联选择',value:'cascader'}];
const selectParamTypes = [{title:'数据字典',value:'dic'},{title:'自定义字典',value:'custDic'}];
export default {
    genComponentConfig,
    getValidateRules,
    allParamTypes,
    selectParamTypes
}
